package com.tianji.common.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.models.RuntimeOptions;

public class SMSUtils {
    /**
     * 阿里云登录id
     */
    private final static String ACCESS_KEY_ID = "LTAI5tHd4CoJk5Rkyzv7Ff2T";

    /**
     * 阿里云登录密码
     */
    private final static String ACCESS_KEY_SECRET = "2GO9NTuBW9yC70YvO0jqDR6JKEikLr";

    /**
     * 初始化登录阿里云的Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 可指定登录的服务器地址，可参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送短信方法
     * @param phoneNumbers 手机号
     * @param signName 签名
     * @param templateCode  模板编号
     * @param param 模板参数
     * @throws Exception
     */
    public static SendSmsResponse sendMessage(String phoneNumbers, String signName, String templateCode, String param) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = SMSUtils.createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setPhoneNumbers(phoneNumbers)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam(param);
        SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
        System.out.println("短信发送成功，返回结果是："+sendSmsResponse);
        return sendSmsResponse;
    }

    public static void main(String[] args) {
        try {
            SMSUtils.sendMessage("15899813131", "阿里云短信测试", "SMS_154958888", "{\"code\":\"7777\"}");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
